#!/bin/bash
set -ex

# 更新 PEAR 和 PECL 通道
pear upgrade --force PEAR || true
pecl channel-update pecl.php.net

# 安装系统依赖
apt-get update && apt-get install -y \
    git zip unzip libicu-dev libzip-dev libonig-dev libxml2-dev \
    libssl-dev libpng-dev libjpeg-dev libfreetype6-dev libcurl4-openssl-dev \
    libsqlite3-dev libxslt-dev zlib1g-dev libaio-dev libpcre3-dev pkg-config \
    re2c bison autoconf \
    && rm -rf /var/lib/apt/lists/*


# 配置并安装内置扩展
docker-php-ext-configure gd --with-freetype --with-jpeg
docker-php-ext-install bcmath ctype curl fileinfo filter ftp gd gettext iconv intl mbstring \
    mysqli pdo pdo_mysql pdo_sqlite pcntl shmop sockets sysvsem tokenizer xml xmlrpc soap \
#    sqlite3 dom xmlreader xmlwriter

# PECL 扩展安装（使用 yes 自动确认）
yes '' | pecl install mongodb-1.8.2
yes '' | pecl install msgpack-2.2.0
yes '' | pecl install redis-5.3.7
yes '' | pecl install swoole-4.8.10
yes '' | pecl install yar-2.3.2

# pecl_http 安装顺序：先装依赖
yes '' | pecl install raphf-2.0.1
docker-php-ext-enable raphf
yes '' | pecl install propro-2.1.0
docker-php-ext-enable propro
yes '' | pecl install pecl_http-3.2.4

# 启用扩展
docker-php-ext-enable mongodb
docker-php-ext-enable msgpack
docker-php-ext-enable redis
docker-php-ext-enable swoole
docker-php-ext-enable yar
docker-php-ext-enable http


# 正确的清理步骤（避免删除 .so）
#find /usr/src/php/ext -name '*.gcno' -o -name '*.gcda' -o -name '*.lo' -o -name '*.o' -o -name '*.la' | xargs rm -f
#find /usr/src/php/ext -name .libs -type d | xargs rm -rf